export default {
  name: "DataEntity",
  data() {
    return {
      //需要设置的基本参数
      currentCode: null,
      currentAPI: null,
      currentBaseURL: null,

      //列表操作相关属性
      showSearch: true,
      ids: [],
      selections: [],
      singleDisable: true,
      multipleDisable: true,

      //列表数据查询相关
      loading: true,
      queryForm: "queryForm",
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      list: [],
      total: 0,

      //维护表单相关
      title: null,
      defaultForm: { id: null },
      form: { id: null },
      dataForm: "form",
      formVisible: false,
    };
  },
  created() {
    const { currentAPI, currentBaseURL, currentCode } = this;
    if (!currentAPI || !currentBaseURL || !currentCode) {
      console.error("基本参数未设置:currentAPI/currentBaseURL/currentCode");
      return;
    }
    this.queryPage();
  },
  methods: {
    refresh() {
      this.queryPage();
    },
    listLoaded(list) {
      this.list = [].concat(list);
    },
    queryPage() {
      this.loading = true;
      this.currentAPI.page(this.queryParams).then((res) => {
        res.rows.forEach((item, index) => {
          item._index = index + 1;
        });
        this.listLoaded(res.rows);
        this.total = res.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryPage();
    },
    resetQuery() {
      this.resetForm(this.queryForm);
      this.handleQuery();
      console.warn(this.currentCode + "reset query use default method");
    },
    handleSelectionChange(selection) {
      this.selections = [].concat(selection);
      this.ids = selection.map((item) => item.id);
      this.singleDisable = selection.length !== 1;
      this.multipleDisable = selection.length === 0;
    },
    afterAdd() {},
    handleAdd() {
      this.formVisible = true;
      this.resetForm(this.dataForm);
      this.form = Object.assign({}, this.defaultForm);
      this.title = this.$t("Create");
      this.afterAdd();
    },
    handleUpdate(row) {
      this.formVisible = true;
      this.resetForm(this.dataForm);
      const id = row.id || this.ids;
      this.currentAPI.oneById(id).then((response) => {
        this.form = response.data;
        this.title = this.$t("Modify");
      });
    },
    formReset() {
      console.error("form reset is not rewrite");
    },
    formCancel() {
      this.formVisible = false;
      this.resetForm(this.dataForm);
    },
    formSubmit() {
      const { currentAPI } = this;
      this.$refs[this.dataForm].validate((valid) => {
        if (!valid) {
          return;
        }
        if (this.form.id) {
          currentAPI.update(this.form).then((response) => {
            this.$modal.msgSuccess(this.$t("ModifySuccessful"));
            this.formVisible = false;
            this.queryPage();
          });
          return;
        }
        currentAPI.create(this.form).then((response) => {
          this.$modal.msgSuccess(this.$t("AddSuccessful"));
          this.formVisible = false;
          this.queryPage();
        });
      });
    },
    handleDelete(row) {
      const { currentAPI } = this;
      const id = row.id || this.ids;
      this.$modal
        .confirm(this.$t("common.SureDelete"))
        .then(function () {
          return currentAPI.deleteById(id);
        })
        .then(() => {
          this.queryPage();
          this.$modal.msgSuccess(this.$t("DeleteSuccessful"));
        })
        .catch(() => {});
    },
    handleExport() {
      this.download(
        this.currentBaseURL + "/export",
        {
          ...this.queryParams,
        },
        this.currentCode + `_${new Date().getTime()}.xlsx`
      );
    },
  },
};
